import { wsg2mapcoor, mapcoor2wsg } from '@/utils/gps/maptransform'
export const fenceDecode = (area: any) => {
  // eslint-disable-next-line
  const point = area.match(/[^\(\)]+(?=\))/g)[0].split(', ')
  if (area.match('CIRCLE')) {
    const p = wsg2mapcoor(point[0].split(' ')[1], point[0].split(' ')[0])
    return {
      type: 'CIRCLE',
      path: [{ lng: p.lng, lat: p.lat }, point[1]]
    }
  }
  if (area.match('POLYGON')) {
    const path: any = []
    point.forEach((item: any) => {
      const p = wsg2mapcoor(item.split(' ')[1], item.split(' ')[0])
      path.push({ lng: p.lng, lat: p.lat })
    })
    return {
      type: 'POLYGON',
      path: path
    }
  }
  if (area.match('LINESTRING')) {
    const path: any = []
    point.forEach((item: any) => {
      const p = wsg2mapcoor(item.split(' ')[1], item.split(' ')[0])
      path.push({ lng: p.lng, lat: p.lat })
    })
    return {
      type: 'LINESTRING',
      path: path
    }
  }
  if (area.match('ADMINISTRATIVE')) {
    // const path = []
    const _ss = point[0].split(',')
    let cascader: any = []
    let city = null
    let level
    _ss.forEach((item: any, index: number) => {
      if (item !== ':') {
        cascader.push(Number(item.split(':')[1]))
        city = item.split(':')[0]
      }
      if (index === 1) {
        level = 'province'
      }
      if (index === 2) {
        level = 'city'
      }
      if (index === 3) {
        level = 'district'
      }
    })
    return {
      type: 'Division',
      cascader,
      city,
      level
    }
  }
}
export const fenceEncode = (tool: string, circleInfo: any, polygonPath: any, linePath: any) => {
  let area = ''
  if (tool === 'Circle') {
    const center = circleInfo.center
    const radius = circleInfo.radius
    const p = mapcoor2wsg(center[0], center[1])
    area = `CIRCLE (${p.lat} ${p.lng}, ${radius})`
  } else if (tool === 'Polygon') {
    const path = polygonPath
    const pathArr = [] as any
    path.forEach((item: any) => {
      const p = mapcoor2wsg(item[0], item[1])
      pathArr.push(`${p.lat} ${p.lng}`)
    })
    area = `POLYGON ((${pathArr.join(', ')}))`
  } else if (tool === 'LineString') {
    const path = linePath
    // console.log(path)
    const pathArr = [] as any
    path.forEach((item: any) => {
      const p = mapcoor2wsg(item[0], item[1])
      pathArr.push(`${p.lat} ${p.lng}`)
    })
    // console.log(pathArr)
    area = `LINESTRING ((${pathArr.join(', ')}))`
  } else if (tool === 'Division') {
    // area = this.area
  }
  return area
}
export function getCenterPoint(path) {
  let lng = 0.0
  let lat = 0.0
  for (let i = 0; i < path.length; i++) {
    lng = lng + parseFloat(path[i].lng)
    lat = lat + parseFloat(path[i].lat)
  }
  lng = lng / path.length
  lat = lat / path.length
  return { lng, lat }
}
export function getCenterPoint1(path) {
  let lng = 0.0
  let lat = 0.0
  for (let i = 0; i < path.length; i++) {
    const p = mapcoor2wsg(path[i][0], path[i][1])
    lng = lng + parseFloat(p.lng)
    lat = lat + parseFloat(p.lat)
  }
  lng = lng / path.length
  lat = lat / path.length
  return { lng, lat }
}
